﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Textbook
{
    public partial class ProForm : Form
    {
        public ProForm(Home home, Procure procure)
        {
            InitializeComponent();
            this.home = home;
            this.procure = procure;
            getLess();
        }

        Home home;

        Procure procure;

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 &&
                MessageBox.Show("确定采购该书籍？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string l_id = dataGridView1.Rows[e.RowIndex].Cells["缺书单号"].Value.ToString();
                int book_id = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["书籍编号"].Value.ToString());
                int num = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["数量"].Value.ToString());
                if (sql.updateLessState(l_id,book_id, num, 1))
                {
                    MessageBox.Show("采购成功...");
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                }
                else
                {
                    MessageBox.Show("采购失败...");
                }
            }
        }

        SQL sql = new SQL();
        bool e1 = true;
        private void getLess()
        {
            //缺书单
            DataSet ds = sql.selectLess(0);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
                if (e1)
                {
                    DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                    column.Text = "采 购";
                    column.Name = "编 辑";
                    column.UseColumnTextForButtonValue = true;
                    dataGridView1.Columns.Add(column);
                    e1 = false;
                }
            }
        }
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex==0)
            {
                getLess();
            }
            else
            {
                if (MessageBox.Show("确定要退出...", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    home.openForm(new Login(home));
                }
            }
        }
    }
}
